package com.occludens.admin.datamodel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Load;
import com.googlecode.objectify.annotation.Subclass;
import com.occludens.admin.ui.Channel;

@Subclass(index=true)
@Cache
public class Attribute extends AdminObjectDataModel {
	public static enum typeList {
		type_boolean,
		type_date,
		type_integer,
		type_string;
	};

	@Index private Attribute.typeList type;
	@Load private List<Ref<Range>> ranges = new ArrayList<Ref<Range>>();
	@Load private List<Ref<Trigger>> triggers = new ArrayList<Ref<Trigger>>();

	protected Attribute() {} // Mandatory for Objectify

	public Attribute(String name) {
		super(name);
	}
	
	public Attribute getAttribute(String name) {
		return ObjectifyService.ofy().load().type(Attribute.class).filter("name", name).first().now();
	}

	public Attribute.typeList getType() {
		return this.type;
	}

	public void setType(Attribute.typeList type) {
		this.type = type;
	}

	public List<Range> getRanges() {
		List<Range> returnList = new ArrayList<Range>();
		Iterator<Ref<Range>> rangesItr = this.ranges.iterator();
		while (rangesItr.hasNext()) {
			returnList.add(rangesItr.next().get());
		}//End of while rangesItr
		return returnList;
	}

	public void setItems(List<Range> ranges) {
		Iterator<Range> rangesItr = ranges.iterator();
		while (rangesItr.hasNext()) {
			Ref<Range> rangeRef = Ref.create(rangesItr.next());
			if (!this.ranges.contains(rangeRef)) {
				this.ranges.add(rangeRef);
			}
		}//End of while rangesItr
	}

	public void addRanges(Range... ranges) {
		for (int i=0; i<ranges.length; i++) {
			Ref<Range> rangeRef = Ref.create(ranges[i]);
			if (!this.ranges.contains(rangeRef)) {
				this.ranges.add(rangeRef);
			}
		}
	}

	public void removeRanges(Range... ranges) {
		for (int i=0; i<ranges.length; i++) {
			Ref<Range> rangeRef = Ref.create(ranges[i]);
			if (this.ranges.contains(rangeRef)) {
				this.ranges.remove(rangeRef);
			}
		}
	}
	
	public List<Trigger> getTriggers() {
		List<Trigger> returnList = new ArrayList<Trigger>();
		Iterator<Ref<Trigger>> triggersItr = this.triggers.iterator();
		while (triggersItr.hasNext()) {
			returnList.add(triggersItr.next().get());
		}//End of while triggersItr
		return returnList;
	}

	public void setTriggers(List<Trigger> triggers) {
		Iterator<Trigger> triggersItr = triggers.iterator();
		while (triggersItr.hasNext()) {
			Ref<Trigger> triggerRef = Ref.create(triggersItr.next());
			if (!this.triggers.contains(triggerRef)) {
				this.triggers.add(triggerRef);
			}
		}//End of while rangesItr
	}

	public void addTriggers(Trigger... triggers) {
		for (int i=0; i<triggers.length; i++) {
			Ref<Trigger> triggerRef = Ref.create(triggers[i]);
			if (!this.triggers.contains(triggerRef)) {
				this.triggers.add(triggerRef);
			}
		}
	}

	public void removeTriggers(Trigger... triggers) {
		for (int i=0; i<triggers.length; i++) {
			Ref<Trigger> triggerRef = Ref.create(triggers[i]);
			if (this.triggers.contains(triggerRef)) {
				this.triggers.remove(triggerRef);
			}
		}
	}

}
